package com.rocky.study.volatitle;


import java.util.concurrent.TimeUnit;

class myDate {
    /**volatile 它会控制被修饰的变量在内存操作上主动把值刷新到主内存,JMM会把该线程对应的CPU内存设置过期,从主内存中读取最新值*/
    //    volatile int number = 0;
    int number = 0;

    void methodExchange() {
        this.number = 100;
    }
}
public class VolatileDemo2 {
    public static void main(String[] args) {
        myDate myDate = new myDate();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t come in");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            myDate.methodExchange();
            System.out.println(Thread.currentThread().getName() + "\t update number value:" + myDate.number);
        },"t1").start();
        // Q:主线程的值是先打印的
        System.out.println(Thread.currentThread().getName() + "\t the number value:" + myDate.number);
        while(myDate.number == 0) {
            // Q:the number value and 验证 print result is oddly
//            System.out.println("====验证====");
//            System.out.println(Thread.currentThread().getName() + "\t number value:" + myDate.number);
        }

        System.out.println(Thread.currentThread().getName() + "\t over");
    }
}
